﻿#region Copyright

// All Material Copyright © 2010 Dynamic Concepts Development Corp., All Rights Reserved.
//      Portions Copyright © 1984-2009 Dynamic Concepts Development Corp., All Rights Reserved.
// For further information please contact: SoftwareSupport@dynconcepts.com
// Dynamic Concepts Development Corp.  311 W 57th St. Suite #422, New York, NY 10019

#endregion

#region Using References

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using DynConDevCorp.OpenArch.DynAppFramework.Tiers.UserInterface.WPF.Engine;

#endregion

namespace DynConDevCorp.OpenArch.DynAppFramework.Tiers.UserInterface.WPF.UIElements
{
    /// <summary>
    /// Interaction logic for TitledImage.xaml
    /// </summary>
    public partial class TitledImage : UserControl
    {
        #region Constructors

        public TitledImage()
        {
            InitializeComponent();
        }

        #endregion

        #region Public Properies and Indexers

        public ICommandHandler CommandHandler
        {
            get { return (ICommandHandler) GetValue(CommandHandlerProperty); }
            set { SetValue(CommandHandlerProperty, value); }
        }


        //////public RoutedUICommand Command
        //////{
        //////    get { return (RoutedUICommand)GetValue(CommandProperty); }
        //////    set { SetValue(CommandProperty, value); }
        //////}

        //////public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
        //////    "Command",
        //////    typeof(RoutedUICommand),
        //////    typeof(TitledImage),
        //////    new PropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));

        //////private static void OnCommandChanged(object sender, DependencyPropertyChangedEventArgs args)
        //////{
        //////    TitledImage instance = (TitledImage)sender;
        //////}


        public ImageSource Graphic
        {
            get { return (ImageSource) GetValue(GraphicProperty); }
            set { SetValue(GraphicProperty, value); }
        }

        public string Title
        {
            get { return (string) GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        #endregion

        #region Public Methods

        public void InternalCommandCanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (CommandHandler != null)
                CommandHandler.CommandCanExecute(sender, args);
            else
            {
                args.CanExecute = false;
                args.ContinueRouting = true;
                args.Handled = false;
            }
        }

        public void InternalCommandExecuted(object sender, ExecutedRoutedEventArgs args)
        {
            if (CommandHandler != null)
                CommandHandler.CommandExecuted(sender, args);
        }

        #endregion

        #region Private Methods

        private static void OnCommandHandlerChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            TitledImage instance = (TitledImage) sender;
            ICommandHandler commandHandler = (ICommandHandler) args.NewValue;
            //////instance.Command = commandHandler.Command;
        }

        private static void OnGraphicChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            TitledImage instance = (TitledImage) sender;
            instance.imgGraphic.Source = (ImageSource) args.NewValue;
        }

        private static void OnTitleChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            TitledImage instance = (TitledImage) sender;
            instance.txtTitle.Text = (string) args.NewValue;
        }

        private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
        {
            m_NormalBorderBrush = BorderBrush;
            BorderBrush = new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));
        }


        private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
        {
            txtTitle.FontWeight = FontWeights.Normal;
            if (m_NormalBorderBrush != null)
            {
                BorderBrush = m_NormalBorderBrush;
                m_NormalBorderBrush = null;
            }
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            m_ClickPending = true;
            txtTitle.FontWeight = FontWeights.Bold;
        }

        private void StackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            txtTitle.FontWeight = FontWeights.Normal;
            if (m_ClickPending)
            {
                InternalCommand.Execute(null, this);
            }
        }

        #endregion

        #region Static Fields and Constants

        public static readonly DependencyProperty CommandHandlerProperty = DependencyProperty.Register(
            "CommandHandler",
            typeof (ICommandHandler),
            typeof (TitledImage),
            new PropertyMetadata(new PropertyChangedCallback(OnCommandHandlerChanged)));

        public static readonly DependencyProperty GraphicProperty = DependencyProperty.Register(
            "Graphic",
            typeof (ImageSource),
            typeof (TitledImage),
            new PropertyMetadata(new PropertyChangedCallback(OnGraphicChanged)));

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
            "Title",
            typeof (string),
            typeof (TitledImage),
            new PropertyMetadata(new PropertyChangedCallback(OnTitleChanged)));

        #endregion

        #region Instance Fields (ReadOnly)

        private readonly RoutedUICommand InternalCommand = EngineCommand.InternalCommand;

        #endregion

        #region Instance Fields (Mutable)

        private bool m_ClickPending;
        private Brush m_NormalBorderBrush;

        #endregion
    }
}